<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt>
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面出题区域 -->
    <el-container>
      <!-- 左侧主体区域 -->
      <el-aside :width="isCode?'64px':'200px'">
        <div class="yasuo" @click="shousuo">||||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCode"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu :index="item.id+''" v-for="item in getList" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+item.path"
              v-for="item in item.children"
              :key="item.id"
              @click="saveactivePath('/'+item.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      activePath: '',
      getList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCode: false
    }
  },
  created() {
    this.getItem()
    this.activePath = window.sessionStorage.getItem('active')
  },
  methods: {
    // 点击退出
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 路由导航守卫
    async getItem() {
      const { data: res } = await this.$http.get('menus')
      // 如果获取接口的状态吗不是200
      if (res.meta.status !== 200) return this.$message.error(res.meta.status)
      this.getList = res.data
    },
    // 导航栏折叠
    shousuo() {
      this.isCode = !this.isCode
    },
    // 点击获取当前的链接
    saveactivePath(active) {
      // 把路由存起来
      window.sessionStorage.setItem('active', active)
      this.activePath = active
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.yasuo {
  color: #fff;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>
